The code is not originally written by me. I am citing the source from which I included the code (http://machinelearningmastery.com/machine-learning-in-python-step-by-step/)- to learn and apply the Zoo Data on the RandomForestClassifier model


In [11]:
#importing pandas, scikit-learn package and the RandomForestClassifier
import pandas
import numpy as np
from pandas.tools.plotting import scatter_matrix
from sklearn import cross_validation
from sklearn.ensemble import RandomForestClassifier
import warnings
warnings.filterwarnings('ignore')

#reading Zoo Dataset from csv file.
zoo = r'C:\Users\priyu\Machine-Learning\zoo-animal-classification\zoo.csv'
training_set = pandas.read_csv(zoo,index_col = False)
#zoo_class_set = pandas.read_csv(zoo_class, index_col = False)
zoo_data_df = training_set[['hair','feathers','eggs','milk','airborne','aquatic','predator','toothed','backbone','breathes','venomous','fins','legs','tail','domestic','catsize']]
zoo_target_df = training_set[['class_type']]
zoo_target = zoo_target_df.values
zoo_data = zoo_data_df.values

In [12]:
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = cross_validation.train_test_split(zoo_data, zoo_target, test_size=validation_size, random_state=seed)

In [13]:
y = Y_train.ravel()
Y_train = np.array(y).astype(int)

In [14]:
# Test options and evaluation metric
num_folds = 10
num_instances = len(X_train)
seed = 7
scoring = 'accuracy'

In [15]:
model = RandomForestClassifier(class_weight='balanced')
results = []
names = []
kfold = cross_validation.KFold(n=num_instances, n_folds=num_folds, random_state=seed)
cv_results = cross_validation.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
results.append(cv_results)
#names.append(name)
msg = "%s: %f (%f)" % ('Random Forest Classifier', cv_results.mean(), cv_results.std())
print(msg)
print(cv_results)
#print(cv_results)
#print(cross_validation.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring))


Random Forest Classifier: 0.962500 (0.057282)
[ 1.     1.     1.     0.875  1.     0.875  1.     1.     0.875  1.   ]